package edu.unlv.cs.ladders.entities;

import edu.unlv.cs.ladders.entities.Score;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect ScoreDataOnDemand_Roo_DataOnDemand {
    
    declare @type: ScoreDataOnDemand: @Component;
    
    private Random ScoreDataOnDemand.rnd = new SecureRandom();
    
    private List<Score> ScoreDataOnDemand.data;
    
    public Score ScoreDataOnDemand.getNewTransientScore(int index) {
        edu.unlv.cs.ladders.entities.Score obj = new edu.unlv.cs.ladders.entities.Score();
        obj.setAccepted(new Boolean(true));
        obj.setScore(new Integer(index));
        return obj;
    }
    
    public Score ScoreDataOnDemand.getSpecificScore(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Score obj = data.get(index);
        return Score.findScore(obj.getId());
    }
    
    public Score ScoreDataOnDemand.getRandomScore() {
        init();
        Score obj = data.get(rnd.nextInt(data.size()));
        return Score.findScore(obj.getId());
    }
    
    public boolean ScoreDataOnDemand.modifyScore(Score obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void ScoreDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = edu.unlv.cs.ladders.entities.Score.findScoreEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Score' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<edu.unlv.cs.ladders.entities.Score>();
        for (int i = 0; i < 10; i++) {
            edu.unlv.cs.ladders.entities.Score obj = getNewTransientScore(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
